package com.bjpowernode.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.shop.base.BaseProduct;
import com.bjpowernode.shop.constants.CommonConstants;
import com.bjpowernode.shop.domain.ProdProp;
import com.bjpowernode.shop.domain.ProdPropValue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/prod/spec")
public class ProdPropController extends BaseProduct {

    // 查询产品属性列表 查询属性key+value
    @GetMapping("/page")
    public ResponseEntity<Page<ProdProp>> page(Page<ProdProp> page, ProdProp prodProp) {
        // 分页模糊查询
        Page<ProdProp> propPage = prodPropService.page(
                page,
                new LambdaQueryWrapper<ProdProp>()
                        .like(
                                StringUtils.hasText(prodProp.getPropName()),
                                ProdProp::getPropName,
                                prodProp.getPropName()
                        )
        );

        if (ObjectUtils.isEmpty(propPage))
            return ResponseEntity.ok(null);

        // 从分页数据中获取prop对象列表
        List<ProdProp> propObjList = propPage.getRecords();
        // 获取ids
        List<Long> propIds = propObjList.stream().map(ProdProp::getPropId).collect(Collectors.toList());
        // 根据ids查询value对象集合
        List<ProdPropValue> propValueObjList = prodPropValueService.list(
                new LambdaQueryWrapper<ProdPropValue>()
                        .in(
                                !CollectionUtils.isEmpty(propIds),
                                ProdPropValue::getPropId,
                                propIds
                        )
        );

        // 将values封装到propObj中, forEach = for(Consumer<>消费)
        propObjList.forEach(
                propObj -> {
                    // 过滤value对象集合，[value对象的propid] 和 [当前遍历prop对象的propid] 相等时
                    // , 提取该对象, 并收集为List
                    List<ProdPropValue> propValuesObjList = propValueObjList.stream().filter(
                            valueObj -> valueObj.getPropId().equals(propObj.getPropId())
                    ).collect(Collectors.toList());

                    // key对value,一对多,把value对象集合存入key对象中就行,前端会自行提取name
                    if (!CollectionUtils.isEmpty(propValuesObjList))
                        propObj.setProdPropValues(propValuesObjList);
                }
        );
        return ResponseEntity.ok(propPage);
    }

    // 新增产品属性
    @PostMapping
    @Transactional
    public ResponseEntity<Boolean> saveProdProp(@RequestBody ProdProp prodProp) {
        System.out.println("***prodProp = " + prodProp);

        // 新增产品属性key
        boolean flag = prodPropService.save(
                prodProp.setRule(2).setShopId(CommonConstants.DEFAULT_SHOP_ID));


        /* prodPropValues=prodPropValues=[ProdPropValue(valueId=null, propValue=valueName1, propId=null), 
            ProdPropValue(valueId=null, propValue=valueName2, propId=null), 
            ProdPropValue(valueId=null, propValue=valueName3, propId=null)] */
        // 将前端传来的propValueObjList，拆解，为每个propValueObj赋值key的id
        prodProp.getProdPropValues().forEach(
                propValueObj -> propValueObj.setPropId(prodProp.getPropId())
        );
        
        // 新增产品属性value,将集合中的所有propValueObj添加到表中
        prodPropValueService.saveBatch(prodProp.getProdPropValues());

        return ResponseEntity.ok(true);         
    }

    // 修改属性的key和value
    @PutMapping
    @Transactional
    public ResponseEntity<Boolean> update(@RequestBody ProdProp prop) {
        List<ProdPropValue> propValuesObjList = prop.getProdPropValues();
        /* 
        0: {valueId: 397, propValue: "valueName11", propId: 82}
        1: {valueId: 398, propValue: "valueName22", propId: 82}
        2: {valueId: 399, propValue: "valueName33", propId: 82}
         */
        // 更新ValueObj中的name
        prodPropValueService.saveOrUpdateBatch(propValuesObjList);
        
        return ResponseEntity.ok(prodPropService.saveOrUpdate(prop));
    }

    // 删除标签
    @DeleteMapping("/{id}")
    @Transactional
    public ResponseEntity<Boolean> del(@PathVariable Integer id) {
        prodPropValueService.remove(
                new LambdaQueryWrapper<ProdPropValue>()
                        .eq(ProdPropValue::getPropId,id)
        );
        return ResponseEntity.ok(prodPropService.removeById(id));
    }
    
    // 展示产品属性列表1***********规格key列表
    @GetMapping("/list")
    public ResponseEntity<List<ProdProp>> list(){
        return ResponseEntity.ok(prodPropService.list());
    }
    
    // 展示产品属性列表2***********规格value列表
    @GetMapping("/listSpecValue/{id}")
    public ResponseEntity<List<ProdPropValue>> list(@PathVariable Integer id){
        return ResponseEntity.ok(prodPropValueService.list(
                new LambdaQueryWrapper<ProdPropValue>().eq(ProdPropValue::getPropId,id)
        ));
    }
}
